﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Expo.Training.DataAccess.Activity;
using Expo.Training.BRule;
using System.Web.UI.HtmlControls;

public partial class Activity_AddActivityCourse : PageBase
{
    protected Guid _activityID;
    private ActivityEntity _mdlActivity;
    const string CourseV2PageIndex = "CourseV2_Manage_Default";

    #region 课程查询参数
    private string SortExpression
    {
        get
        {
            if (this.ViewState["SortExpression"] == null || this.ViewState["SortExpression"].ToString().Trim().Length == 0)
                this.ViewState["SortExpression"] = "AddTime DESC";
            return this.ViewState["SortExpression"].ToString().Trim();
        }
        set
        {
            this.ViewState["SortExpression"] = value;
        }
    }
    public string CourseName
    {
        get
        {
            if (ViewState["CourseName"] == null)
            {
                return string.Empty;
            }
            else
            {
                return ViewState["CourseName"].ToString();
            }
        }
        set
        {
            ViewState["CourseName"] = value;
        }
    }
    public string SerialNumber
    {
        get
        {
            if (ViewState["SerialNumber"] == null)
            {
                return string.Empty;
            }
            else
            {
                return ViewState["SerialNumber"].ToString();
            }
        }
        set
        {
            ViewState["SerialNumber"] = value;
        }
    }
    public string CreatorName
    {
        get
        {
            if (ViewState["CreatorName"] == null)
            {
                return string.Empty;
            }
            else
            {
                return ViewState["CreatorName"].ToString();
            }
        }
        set
        {
            ViewState["CreatorName"] = value;
        }
    }
    public DateTime CreateDate
    {
        get
        {
            if (ViewState["CreateDate"] == null)
            {
                return DateTime.Parse("1900-1-1");
            }
            else
            {
                return DateTime.Parse(ViewState["CreateDate"].ToString());
            }
        }
        set
        {
            ViewState["CreateDate"] = value;
        }
    }
    public int CourseType
    {
        get
        {
            if (ViewState["CourseType"] == null)
            {
                return 0;
            }
            else
            {
                return Int32.Parse(ViewState["CourseType"].ToString());
            }
        }
        set
        {
            ViewState["CourseType"] = value;
        }
    }
    private Guid SelectedID
    {
        get
        {
            if (ViewState["SelectedID"] != null)
                return new Guid(ViewState["SelectedID"].ToString());
            return Guid.Empty;
        }
        set { ViewState["SelectedID"] = value; }
    }


    public int Include
    {
        get
        {
            if (ViewState["Include"] == null)
            {
                return 0;
            }
            else
            {
                return int.Parse(ViewState["Include"].ToString());
            }
        }
        set
        {
            ViewState["Include"] = value;
        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        _activityID = new Guid(Request.QueryString["id"].ToString());
        _mdlActivity = ActivityModule.GetActivity(_activityID);

        if (!IsPostBack)
        {
            

            string pageindex = CookieManager.ReadCookie(CourseV2PageIndex);
            if (!string.IsNullOrEmpty(pageindex))
            {
                this.cc_ExPager.PageIndex = int.Parse(pageindex);
            }
            Bind();
            this.ltlActivityTitle.Text = _mdlActivity.ActivityTitle;
        }

        
    }

    private void Bind()
    {
        bool isLastest = this.chk_islastest.Checked;
        bool isRecommend = this.chk_isrecommend.Checked;
        int totalCount = 0;
        this.rpt_CourseList.DataSource = Expo.Training.DataAccess.Course.CourseV2.GetCourseListFrom(isLastest, isRecommend, this.Member.CustomerID, this.CourseName, this.SerialNumber, this.CreatorName, this.CreateDate, this.SortExpression, this.cc_ExPager.PageIndex + 1, this.cc_ExPager.PageSize, out totalCount, true, 0, this.CourseType, CatalogSelector2.SelectedID, this.Include);
        this.rpt_CourseList.DataBind();
        this.cc_ExPager.TotalRecords = totalCount;
    }

    protected void PageIndex_Changed(object sender, EventArgs e)
    {
        int pageindex = this.cc_ExPager.PageIndex;
        CookieManager.WriteCookie(CourseV2PageIndex, pageindex.ToString(), DateTime.Now.AddDays(1));
        Bind();
    }

    #region 搜索课程
    protected void btnSearch_ServerClick(object sender, EventArgs e)
    {
        this.cc_ExPager.PageIndex = 0;
        this.CourseType = int.Parse(ddlType.SelectedValue);
        this.CourseName = txt_CourseName.Text.Trim().Replace("'", "''");
        this.SerialNumber = txt_SerialNumber.Text.Trim().Replace("'", "''");
        this.CreatorName = txt_CreatorName.Text.Trim().Replace("'", "''");
        this.SelectedID = CatalogSelector2.SelectedID;

        if (this.ck_Sub.Checked)
        {
            this.Include = 1;
        }
        else
        {
            this.Include = 0;
        }

        DateTime dtime = DateTime.Parse("1900-1-1");
        if (txt_CreateDate.Text.Trim().Length > 0)
        {
            if (!DateTime.TryParse(this.txt_CreateDate.Text.Trim(), out dtime))
            {
                dtime = DateTime.Parse("1900-1-1");
            }
        }

        this.CreateDate = dtime;

        this.SortExpression = string.Empty;
        this.Bind();

        this.txt_CourseName.Text = this.txt_SerialNumber.Text = this.txt_CreatorName.Text = this.txt_CreateDate.Text = string.Empty;
    }
    #endregion

    #region 关联课程
    protected void btnRelate_Click(object sender, EventArgs e)
    {
        int _total = 0;
        foreach (RepeaterItem _item in rpt_CourseList.Items)
        {
            HtmlInputCheckBox ck_One = (HtmlInputCheckBox)_item.FindControl("ck_One");
            if (ck_One.Checked)
            {
                Guid _courseID = new Guid(ck_One.Value);

                if (!ActivityModule.IsExistOfCourse(_activityID, _courseID))
                {
                    ActivityModule.AddActivityCourse(_activityID, _courseID);
                    _total++;
                }
            }
        }

        AlertAndReflesh(this.Page, "成功关联了" + _total.ToString() + "门课程！");
    }
    #endregion

}
